package com.ruoyi.system.controller.transport;

import com.ruoyi.system.common.BillTypeConstant;
import com.ruoyi.system.common.TransportCompanyType;
import com.ruoyi.system.domain.db.financial.FinancialEconomic;
import com.ruoyi.system.domain.db.transport.TransportTask;
import com.ruoyi.system.domain.request.TradeCargoOrderDeliveryRequest;
import com.ruoyi.system.domain.vo.PageInfo;
import com.ruoyi.system.domain.vo.TransportTaskVo;
import com.ruoyi.system.service.IFinancialEconomicService;
import com.ruoyi.system.service.ITransportTaskService;
import com.ruoyi.system.utils.MyPageUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;

/**
 * 运输任务Controller
 * 
 * @author ruoyi
 * @date 2023-08-03
 */
@RestController
@RequestMapping("/transport/task")
public class TransportTaskController extends BaseController
{
    @Autowired
    private ITransportTaskService transportTaskService;

    @Autowired
    private IFinancialEconomicService financialEconomicService;

    /**
     * 查询管理员所属的运输任务列表
     */
    @PreAuthorize("@ss.hasPermi('transport:task:list')")
    @GetMapping("/list")
    public PageInfo<TransportTaskVo> list(TransportTask transportTask)
    {
        //startPage();
        return MyPageUtils.startPage(transportTaskService.selectTransportTaskList(transportTask));
    }


    /**
     * 获取运输任务详细信息
     */
    @PreAuthorize("@ss.hasPermi('transport:task:query')")
    @GetMapping(value = "/{taskId}")
    public AjaxResult getInfo(@PathVariable("taskId") Long taskId)
    {
        return success(transportTaskService.selectTransportTaskByTaskId(taskId));
    }

    /**
     * 新增运输任务 （不需要id)
     */
    @PreAuthorize("@ss.hasPermi('transport:task:add')")
    @Log(title = "运输任务", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TransportTask transportTask)
    {
        return toAjax(transportTaskService.insertTransportTask(transportTask));
    }

    /**
     * 修改运输任务
     */
    @PreAuthorize("@ss.hasPermi('transport:task:edit')")
    @Log(title = "运输任务", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TransportTask transportTask)
    {
        return toAjax(transportTaskService.updateTransportTask(transportTask));
    }

    /**
     * 删除运输任务
     */
    @PreAuthorize("@ss.hasPermi('transport:task:remove')")
    @Log(title = "运输任务", businessType = BusinessType.DELETE)
	@DeleteMapping("/{taskIds}")
    public AjaxResult remove(@PathVariable Long[] taskIds)
    {
        return toAjax(transportTaskService.deleteTransportTaskByTaskIds(taskIds));
    }

    /**
     * 获取仓库列表
     */
    @PreAuthorize("@ss.hasPermi('transport:task:warehouselist')")
    @GetMapping("/getWarehouseList")
    public AjaxResult getWarehouseList()
    {
        return AjaxResult.success(transportTaskService.getWarehouseList());
    }

    /**
     * 获取未分配的运输任务列表
     */
    @PreAuthorize("@ss.hasPermi('transport:task:unassignedlist')")
    @GetMapping("/getUnassignedList")
    public AjaxResult getUnassignedList()
    {
        return AjaxResult.success(transportTaskService.selectUndeliveriedOrderList());
    }

    /**
     * 分配运输任务
     * 此处任务派出去之后不要删除，删除那里账单没有处理
     */
    @PreAuthorize("@ss.hasPermi('transport:task:assign')")
    @PostMapping("/assign")
    public AjaxResult assign(@RequestBody TradeCargoOrderDeliveryRequest tradeCargoOrderDeliveryRequest)
    {
        int result = transportTaskService.assignOrderToTask(tradeCargoOrderDeliveryRequest);
        Integer type = transportTaskService.selectTransportTaskByTaskId(tradeCargoOrderDeliveryRequest.getTaskId()).getType();
        if (TransportCompanyType.EXTERNAL == type) { // 外部承运商订单
            FinancialEconomic financialEconomic = new FinancialEconomic(
                    BillTypeConstant.COOPERATION_TRANSPORT,tradeCargoOrderDeliveryRequest.getTaskId(),
                    tradeCargoOrderDeliveryRequest.getSalary()*(-1),tradeCargoOrderDeliveryRequest.getRemark() // 备注从remark改过去
            );
            financialEconomicService.insertFinancialEconomic(financialEconomic);
        }
        return success(result);
    }

    /**
     * 获取可用的运输公司列表
     */
    @PreAuthorize("@ss.hasPermi('transport:task:transportcompanylist')")
    @GetMapping("/getTransportCompanyList")
    public AjaxResult getTransportCompanyList()
    {
        return AjaxResult.success(transportTaskService.getTransportCompanyList());
    }

    /**
     * 获取可用的汽车
     */
    @PreAuthorize("@ss.hasPermi('transport:task:carlist')")
    @GetMapping("/getCarList")
    public AjaxResult getCarList()
    {
        return AjaxResult.success(transportTaskService.getAvailableCarList());
    }

}
